from sqlalchemy.orm import Session
from schema import Task_Plot_schema
from model import Task_Plot_model



def get_Task_Plot(db: Session, ID: int):
    return db.query(Task_Plot_model.Task_plot).filter(Task_Plot_model.Task_plot.ID == ID).first()


def get_all_Task_Plot(db: Session):
    return db.query(Task_Plot_model.Task_plot).all()


def delete_Task_Plot(db: Session, ID):
    to_delete = db.query(Task_Plot_model.Task_plot).filter(Task_Plot_model.Task_plot.ID == ID)
    deleted = to_delete.delete()
    if deleted == 0:
        return None
    else:
        db.commit()
        return True


def create_Task_Plot(db: Session, Task_Plot: Task_Plot_schema.Task_plot):
    db_Task_Plot = Task_Plot_model.Task_plot(
        PlotID=Task_Plot.PlotID,
        OperationID=Task_Plot.OperationID,
        Remarks=Task_Plot.Remarks,
    )
    db.add(db_Task_Plot)
    db.commit()
    db.refresh(db_Task_Plot)
    return db_Task_Plot


def update_Task_Plot(db: Session, ID, field_name, new_value):
    db_Task_Plot = db.query(Task_Plot_model.Task_plot).filter(Task_Plot_model.Task_plot.ID == ID).first()
    setattr(db_Task_Plot, field_name, new_value)
    db.commit()
    db.refresh(db_Task_Plot)
    return db_Task_Plot
